//---------------------------------------------------------------------
// Author: Keith Hill
//
// Description: Class to implement the Get-ShortPath cmdlet which is 
//              used to get the short names for the components of a 
//              path.
//
// Creation Date: Nov 23, 2006
//---------------------------------------------------------------------
using System;
using System.ComponentModel;
using System.IO;
using System.Management.Automation;
using System.Text;
using Pscx;
using Pscx.Commands.Internal;
using Pscx.Interop;

namespace Pscx.Commands.FileSystem
{
    [Cmdlet(VerbsCommon.Get, PscxNouns.ShortPath, DefaultParameterSetName = "Path"),
     Description("Gets the short, 8.3 name for the given path."),
     DetailedDescription("Gets the short, 8.3 name for the given path.  This cmdlet emits a ShortPathInfo object that contains a ShortPath property as well as a Path property which contains the original long path.")]
    public class GetShortPathCommand : PscxPathCommandBase
    {
        private StringBuilder _strBld;

        protected override void BeginProcessing()
        {
            _strBld = new StringBuilder(260);
        }

        protected override void ProcessPath(string path)
        {
            _strBld.Length = 0;
            int result = NativeMethods.GetShortPathName(path, _strBld, (uint)_strBld.Capacity);
            if (result > 0)
            {
                ShortPathInfo shortPathInfo = new ShortPathInfo(path, _strBld.ToString());
                WriteObject(shortPathInfo);
            }
            else
            {
                FileNotFoundException ex = new FileNotFoundException();
                WriteError(new ErrorRecord(ex, "InvalidPath", ErrorCategory.InvalidData, path));
            }
        }

        protected override void EndProcessing()
        {
            _strBld = null;
        }
    }
}

